#!/bin/bash
set -e

# Show 环境定时任务部署脚本
# 使用 Kustomize 构建并部署 show 环境的定时任务

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

echo "===== Show 环境定时任务部署 ====="
echo "时间: $(date '+%Y-%m-%d %H:%M:%S')"
echo ""

# 1. 创建 namespace 和基础资源
echo "1. 创建 namespace 和 RBAC..."
kubectl apply -f namespace.yaml
kubectl apply -f rbac.yaml

# 2. 创建 Secrets
echo "2. 创建 Secrets..."
kubectl apply -f secrets.yaml

# 3. 使用 Kustomize 构建并部署 CronJob
echo "3. 部署 CronJob..."
kubectl apply -k .

# 4. 验证部署
echo ""
echo "===== 部署验证 ====="
echo "Namespace:"
kubectl get namespace tasks-show

echo ""
echo "ServiceAccount:"
kubectl get serviceaccount -n tasks-show

echo ""
echo "Secrets:"
kubectl get secrets -n tasks-show

echo ""
echo "CronJobs:"
kubectl get cronjobs -n tasks-show -o wide

echo ""
echo "===== 部署完成 ====="
echo "查看日志命令:"
echo "  kubectl logs -n tasks-show -l app=daily-tasks --tail=100"
echo "  kubectl logs -n tasks-show -l app=line-loss-calculation --tail=100"
echo "  kubectl logs -n tasks-show -l app=device-offline-monitor --tail=100"
echo ""
echo "手动测试命令:"
echo "  kubectl create job test-daily-\$(date +%s) --from=cronjob/daily-tasks -n tasks-show"
echo "  kubectl create job test-lineloss-\$(date +%s) --from=cronjob/line-loss-calculation -n tasks-show"
echo "  kubectl create job test-monitor-\$(date +%s) --from=cronjob/device-offline-monitor -n tasks-show"
